package com.qbw.core.view;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.lib.util.ResUtil;
import com.lib.util.ScreenUtil;
import com.qbw.app.R;
import com.qbw.log.XLog;

/**
 * @author qinbaowei
 * @createtime 2017/11/08 09:44
 * @email qbaowei@qq.com
 * @description
 */


public class ProgressView extends FrameLayout {

    public ProgressView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public ProgressView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ProgressView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attributeSet) {
        ProgressBar progressBar = new ProgressBar(context);
        progressBar.setIndeterminateDrawable(ResUtil.getDrawable(R.drawable.toast_loading));
        progressBar.setIndeterminate(true);
        int size = ScreenUtil.dp2px(context, 20);
        LayoutParams params = new LayoutParams(size, size);
        params.gravity = Gravity.CENTER;
        progressBar.setLayoutParams(params);
        addView(progressBar);
        setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                XLog.w("Invalid click!");
            }
        });
    }

    /**
     * 有一次没在ui线程里面更新view，app又没有抛异常，导致排查了很久
     *
     * @param b
     */
    public void showInUiThread(final boolean b) {
        post(new Runnable() {
            @Override
            public void run() {
                show(b);
            }
        });
    }

    public void show(boolean b) {
        setVisibility(b ? View.VISIBLE : View.GONE);
    }
}
